/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import process from 'process';

import { HvigorLogger } from '../../log/hvigor-log.js';
import { formatTime } from '../../util/time-util.js';
import { hvigorProcess } from '../lifecycle/hvigor-process.js';
import { taskControlCenter } from '../task/core/task-control-center.js';
import { hvigorfileTypeCheck } from '../util/ts_check.js';
import { coreParameter } from '../data/global-core-parameters.js';

const _log = HvigorLogger.getLogger('hvigor-task-executor');

/**
 * 根据DAG图,将传入的参数递归的执行所有任务和其依赖
 *
 * @since 2022/4/22
 */
export class TaskExecutor {
  /**
   * 通过指定task的path，执行对应的任务
   *
   * @param {string[]} taskPaths 需要执行的所有任务路径
   */
  async initTasks(taskPaths: string[]): Promise<void> {
    taskControlCenter.initTaskQueueBeforeRun(taskPaths);
    if (coreParameter.startParams.hvigorfileTypeCheck) {
      hvigorfileTypeCheck();
    }
    logConfigurationCostTime();
  }
}

function logConfigurationCostTime() {
  const taskEndTime: [number, number] = process.hrtime(hvigorProcess.taskBeginTime);
  const realTime = formatTime(taskEndTime);

  _log.debug(`Configuration task cost before running: ${realTime}`);
}

export const taskExecutorInst = new TaskExecutor();
